package com.innovatio.validator.handlers;

import com.innovatio.validator.annotations.ValidateCustom;
import com.innovatio.validator.ValidationErrors;
import com.innovatio.validator.Validable;
import com.innovatio.validator.exceptions.ValidatorException;

/**
 * This code is property of Innovatio Software Solutions, Inc.
 * User: Edxe
 * Date: Jul 6, 2009
 * Time: 6:00:56 AM
 */
public class ValidateCustomHandler implements ValidationHandler<ValidateCustom>{

    @Override
    public ValidationErrors.Error validate(ValidateCustom annotation, Validable container, String fieldName, String value) throws ValidatorException {

        try {
            Class<? extends CustomValidationHandler> c = annotation.handler();
            CustomValidationHandler handler = (CustomValidationHandler)c.newInstance();
            if(!handler.validate(fieldName,value))
                return new ValidationErrors.Error(fieldName,annotation.i18nBundleKey());
            else
                return null;
        } catch (Exception e) {
            throw new ValidatorException(e);
        }

    }
}
